
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Mock模拟数据 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="STYLE.html" />
    
    
    <link rel="prev" href="AJAX.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="START.html">
            
                <a href="START.html">
            
                    
                    快速开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="MENU.html">
            
                <a href="MENU.html">
            
                    
                    菜单配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="PAGE.html">
            
                <a href="PAGE.html">
            
                    
                    页面开发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="ROUTE.html">
            
                <a href="ROUTE.html">
            
                    
                    系统路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="AJAX.html">
            
                <a href="AJAX.html">
            
                    
                    Ajax请求
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.7" data-path="MOCK.html">
            
                <a href="MOCK.html">
            
                    
                    Mock模拟数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="STYLE.html">
            
                <a href="STYLE.html">
            
                    
                    样式&主题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="LAYOUT.html">
            
                <a href="LAYOUT.html">
            
                    
                    导航布局
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="MODEL.html">
            
                <a href="MODEL.html">
            
                    
                    Model(Redux)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="I18N.html">
            
                <a href="I18N.html">
            
                    
                    国际化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="PERMISSION.html">
            
                <a href="PERMISSION.html">
            
                    
                    权限控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="NGINX.html">
            
                <a href="NGINX.html">
            
                    
                    Nginx配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="PROXY.html">
            
                <a href="PROXY.html">
            
                    
                    开发代理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="OTHER.html">
            
                <a href="OTHER.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Mock模拟数据</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="mock-&#x6A21;&#x62DF;&#x6570;&#x636E;">Mock &#x6A21;&#x62DF;&#x6570;&#x636E;</h1>
<p>&#x524D;&#x540E;&#x7AEF;&#x5E76;&#x884C;&#x5F00;&#x53D1;&#xFF0C;&#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x540E;&#x7AEF;&#x5FEB;&#x901F;&#x5F00;&#x53D1;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x7B49;&#x5F85;&#x540E;&#x7AEF;&#x63A5;&#x53E3;&#xFF0C;&#x7CFB;&#x7EDF;&#x63D0;&#x4F9B;&#x4E86;mock&#x529F;&#x80FD;&#x3002;&#x57FA;&#x4E8E;<a href="http://mockjs.com/" target="_blank">mockjs</a></p>
<h2 id="&#x7F16;&#x5199;&#x6A21;&#x62DF;&#x6570;&#x636E;">&#x7F16;&#x5199;&#x6A21;&#x62DF;&#x6570;&#x636E;</h2>
<p>&#x5728;/src/mock&#x76EE;&#x5F55;&#x4E0B;&#x8FDB;&#x884C;mock&#x6570;&#x636E;&#x7F16;&#x5199;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {getUsersByPageSize} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./mockdata/user&apos;</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    <span class="hljs-string">&apos;post /mock/login&apos;</span>: (config) =&gt; {
        <span class="hljs-keyword">const</span> {
            userName,
            password,
        } = <span class="hljs-built_in">JSON</span>.parse(config.data);
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve, reject) =&gt; {
            <span class="hljs-keyword">if</span> (userName !== <span class="hljs-string">&apos;test&apos;</span> || password !== <span class="hljs-string">&apos;111&apos;</span>) {
                setTimeout(() =&gt; {
                    reject({
                        code: <span class="hljs-number">1001</span>,
                        message: <span class="hljs-string">&apos;&#x7528;&#x6237;&#x540D;&#x6216;&#x5BC6;&#x7801;&#x9519;&#x8BEF;&apos;</span>,
                    });
                }, <span class="hljs-number">1000</span>);
            } <span class="hljs-keyword">else</span> {
                setTimeout(() =&gt; {
                    resolve([<span class="hljs-number">200</span>, {
                        id: <span class="hljs-string">&apos;1234567890abcde&apos;</span>,
                        name: <span class="hljs-string">&apos;MOCK &#x7528;&#x6237;&apos;</span>,
                        loginName: <span class="hljs-string">&apos;MOCK &#x767B;&#x5F55;&#x540D;&apos;</span>,
                    }]);
                }, <span class="hljs-number">1000</span>);
            }
        });
    },
    <span class="hljs-string">&apos;post /mock/logout&apos;</span>: {},

    <span class="hljs-string">&apos;get /mock/user-center&apos;</span>: (config) =&gt; {
        <span class="hljs-keyword">const</span> {
            pageSize,
            pageNum,
        } = config.params;


        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve) =&gt; {
            setTimeout(() =&gt; {
                resolve([<span class="hljs-number">200</span>, {
                    pageNum,
                    pageSize,
                    total: <span class="hljs-number">888</span>,
                    list: getUsersByPageSize(pageSize),
                }]);
            }, <span class="hljs-number">1000</span>);
        });
    },
    <span class="hljs-string">&apos;get re:/mock/user-center/.+&apos;</span>: {id: <span class="hljs-number">1</span>, name: <span class="hljs-string">&apos;&#x718A;&#x5927;&apos;</span>, age: <span class="hljs-number">22</span>, job: <span class="hljs-string">&apos;&#x524D;&#x7AEF;&apos;</span>},
    <span class="hljs-string">&apos;post /mock/user-center&apos;</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">&apos;put /mock/user-center&apos;</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">&apos;delete re:/mock/user-center/.+&apos;</span>: <span class="hljs-string">&apos;id&apos;</span>,
}
</code></pre>
<h2 id="&#x7B80;&#x5316;">&#x7B80;&#x5316;</h2>
<p>&#x4E3A;&#x4E86;&#x65B9;&#x4FBF;mock&#x63A5;&#x53E3;&#x7F16;&#x5199;&#xFF0C;&#x7CFB;&#x7EDF;&#x63D0;&#x4F9B;&#x4E86;&#x7B80;&#x5316;&#x811A;&#x672C;(/src/mock/simplify.js)&#xFF0C;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x5C31;&#x662F;&#x7B80;&#x5316;&#x5199;&#x6CD5;</p>
<p>&#x5BF9;&#x8C61;&#x7684;key&#x7531; method url delay&#xFF0C;&#x5404;&#x90E8;&#x5206;&#x7EC4;&#x6210;&#xFF0C;&#x4EE5;&#x7A7A;&#x683C;&#x9694;&#x5F00;</p>
<table>
<thead>
<tr>
<th>&#x5B57;&#x6BB5;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>method</td>
<td>&#x8BF7;&#x6C42;&#x65B9;&#x6CD5; get post&#x7B49;</td>
</tr>
<tr>
<td>url</td>
<td>&#x8BF7;&#x6C42;&#x7684;url</td>
</tr>
<tr>
<td>delay</td>
<td>&#x6A21;&#x62DF;&#x5EF6;&#x8FDF;&#xFF0C;&#x6BEB;&#x79D2; &#x9ED8;&#x8BA4;1000</td>
</tr>
</tbody>
</table>
<h2 id="&#x8C03;&#x7528;">&#x8C03;&#x7528;</h2>
<p>&#x7CFB;&#x7EDF;&#x5C01;&#x88C5;&#x7684;ajax&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4EE5;&#x4E0B;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x81EA;&#x52A8;&#x533A;&#x5206;&#x662F;mock&#x6570;&#x636E;&#xFF0C;&#x8FD8;&#x662F;&#x771F;&#x5B9E;&#x540E;&#x7AEF;&#x6570;&#x636E;&#xFF0C;&#x65E0;&#x9700;&#x5176;&#x4ED6;&#x914D;&#x7F6E;</p>
<p>mock&#x8BF7;&#x6C42;&#xFF1A;</p>
<ul>
<li>url&#x4EE5;/mock/&#x5F00;&#x5934;&#x7684;&#x8BF7;&#x6C42;</li>
<li>/src/mock/url-config.js&#x4E2D;&#x914D;&#x7F6E;&#x7684;&#x8BF7;&#x6C42;</li>
</ul>
<pre><code class="lang-js"><span class="hljs-keyword">this</span>.props.ajax.get(<span class="hljs-string">&apos;/mock/users&apos;</span>).then(...);
</code></pre>
<p>&#x5982;&#x679C;&#x540E;&#x7AEF;&#x771F;&#x5B9E;&#x63A5;&#x53E3;&#x51C6;&#x5907;&#x597D;&#x4E4B;&#x540E;&#xFF0C;&#x53BB;&#x6389;url&#x4E2D;&#x7684;/mock&#x5373;&#x53EF;</p>
<p>&#x6CE8;&#xFF1A;mock&#x529F;&#x80FD;&#x53EA;&#x6709;&#x5F00;&#x53D1;&#x6A21;&#x5F0F;&#x4E0B;&#x542F;&#x7528;&#x4E86;&#xFF0C;&#x751F;&#x4EA7;&#x6A21;&#x5F0F;&#x4E0D;&#x4F1A;&#x5F00;&#x542F;mock&#x529F;&#x80FD;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="AJAX.html" class="navigation navigation-prev " aria-label="Previous page: Ajax请求">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="STYLE.html" class="navigation navigation-next " aria-label="Next page: 样式&主题">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Mock模拟数据","level":"1.7","depth":1,"next":{"title":"样式&主题","level":"1.8","depth":1,"path":"STYLE.md","ref":"STYLE.md","articles":[]},"previous":{"title":"Ajax请求","level":"1.6","depth":1,"path":"AJAX.md","ref":"AJAX.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"MOCK.md","mtime":"2019-02-16T09:08:05.324Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-02-17T03:27:10.809Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

